package cn.org.spring.wechar.utils;

import cn.org.spring.common.cache.Cache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @author： 谢辉
 * @date： 2021/5/8
 * @email: xieh_mail@163.com
 * @description：
 * @modifiedBy：
 * @version: 1.0
 */
@Component
@DependsOn("redisTemplate")
public class CacheUtil implements Cache {
    @Autowired
    RedisTemplate<Object, Object> redisTemplate;

    @Override
    public Set<String> getKeys() {
        Set<Object> keys = redisTemplate.keys("*");
        Set<String> getKeys = keys.stream().map((key) -> {
            return String.valueOf(key);
        }).collect(Collectors.toSet());
        return getKeys;
    }

    @Override
    public Set<String> getKeys(String pattern) {
        Set<Object> keys = redisTemplate.keys(pattern);
        Set<String> getKeys = keys.stream().map((key) -> {
            return String.valueOf(key);
        }).collect(Collectors.toSet());
        return getKeys;
    }

    @Override
    public Boolean exists(String key) {
        return redisTemplate.hasKey(key);
    }

    @Override
    public Boolean del(String key) {
        return redisTemplate.delete(key);
    }

    @Override
    public void set(String key, Object value) {
        redisTemplate.opsForValue().set(key, value);
    }

    @Override
    public void set(String key, Object value, Long expiration) {
        redisTemplate.opsForValue().set(key, value, expiration, TimeUnit.SECONDS);
    }

    @Override
    public void set(String key, Object value, Long expiration, TimeUnit timeUnit) {
        redisTemplate.opsForValue().set(key, value, expiration, timeUnit);
    }

    @Override
    public Object get(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    @Override
    public Boolean expire(String key, Long expire) {
        return redisTemplate.expire(key, expire, TimeUnit.SECONDS);
    }

    @Override
    public void append(String key, String value) {

    }

    @Override
    public String getset(String key, String newValue) {
        return null;
    }

    @Override
    public boolean setnx(String key, String value) {
        return false;
    }

    @Override
    public Long incrBy(String key, Long delta) {
        return null;
    }
}
